<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <div>
            <span>姓名:</span>
            <input type="text" v-model.trim="username" />
        </div>
        <div>
            <span>年龄:</span>
            <input type="number" v-model.number="age" />
        </div>
        <div>
            <span>性别:</span>
            <select v-model="gender">
                <option value="男">男</option>
                <option value="女">女</option>
            </select>
        </div>
        <div>
            <button @click="btn1">添加/修改</button>
        </div>
        <div>
            <table border="1" cellpadding="10" cellspacing="0">
                <tr>
                    <th>序号</th>
                    <th>姓名</th>
                    <th>年龄</th>
                    <th>性别</th>
                    <th>操作</th>
                </tr>
                <tr v-for="(item,index) in list" :key="item.id">
                    <td>{{index+1}}</td>
                    <td>{{item.name}}</td>
                    <td>{{item.age}}</td>
                    <td>{{item.gender? '男': '女'}}</td>
                    <td>
                        <button @click="btn(item.id)">删除</button>
                        <button @click="btn2(item.id)">编辑</button>
                    </td>
                </tr>
            </table>
        </div>
    </div>
    <script src="../vue.js"></script>
    <script>
        const app = new Vue({
            el: '#app',
            data: {
                list: [
                    {
                        id: 1,
                        name: 'Tom',
                        age: 19,
                        gender: 1
                    },
                    {
                        id: 2,
                        name: 'Jone',
                        age: 21,
                        gender: 0
                    },
                ],
                username: '',
                age: 0,
                gender: '',
                student: 0
            },
            methods: {
                // 删除
                btn(id) {
                    this.list = this.list.filter(ele => ele.id !== id)
                },

                btn1() {
                    // 修改
                    if (this.student) {
                        const a = this.list.find(e => e.id === this.student)
                        a.name = this.username
                        a.age = this.age
                        a.gender = this.gender

                    }// 新增
                    else {
                        if (!this.username) {
                            alert('请输入姓名')
                            return
                        }
                        if (this.age < 0) {
                            alert('请输入有效年龄')
                        }
                        if (!this.gender) {
                            alert('请输入性别')
                            return
                        }

                        this.list.push({
                            id: +new Date(),
                            name: this.username,
                            age: this.age,
                            gender: this.gender
                        })

                        this.username = ''
                        this.age = 0
                        this.gender = ''
                        this.student = 0
                    }

                },

                // 编辑
                btn2(id) {
                    const a = this.list.find(ele => ele.id === id)
                    this.username = a.name
                    this.age = a.age
                    this.gender = a.gender ? '男' : '女'
                    this.student = id
                }
            }
        })
    </script>
</body>

</html>